package UserInterface;


import java.awt.AWTEvent;
import java.awt.Adjustable;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.List;

import javax.accessibility.Accessible;
import javax.swing.BorderFactory;
import javax.swing.BoundedRangeModel;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollBar;
import javax.swing.JSpinner;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import Control.LCPDControl;
import Control.LCPDConverterSettings;
import Control.LCPDData;
import Control.LCPDConverterSettings.LCPDTriggerEdge;
import Control.LCPDConverterSettings.LCPDTriggerMode;
/**
 * User Interface of the oscilloscope, this class starts as thread and displays the sampled data, get user inputes, etc.
 * 
 * @author kurmt1@bfh.ch, woodr1@bfh.ch, eichs2@bfh.ch
 * @version 1.0
 */

public class LCPDUserInterface extends JFrame implements Runnable, ActionListener, AdjustmentListener, ChangeListener{

	private LCPDData SamplingData;
	private LCPDConverterSettings ConverterSettings;
	private LCPDGraphicSettings GraphicSettings;
	private LCPDCursor Cursor;
	private LCPDExportData ExportData;
	private LCPDMathematicFunctions MathematicFunctions;
	private LCPDChart Chart;
	private LCPDControl Control;
	
	Float[] VoltageValues={(float) 0.01,
			(float) 0.02,
			(float) 0.05,
			(float) 0.1,
			(float) 0.2,
			(float) 0.5,
			(float) 1.0,
			(float) 2.0,
			(float) 5.0,
			(float) 10.0,
			(float) 20.0};
	Float[] TimeValues={
			(float) 0.0000001,
			(float) 0.0000002,
			(float) 0.0000005,
			(float) 0.000001,
			(float) 0.000002,
			(float) 0.000005,
			(float) 0.00001,
			(float) 0.00002,
			(float) 0.00005,
			(float) 0.0001,
			(float) 0.0002,
			(float) 0.0005,
			(float) 0.001,
			(float) 0.002,
			(float) 0.005,
			(float) 0.01,
			(float) 0.02,
			(float) 0.05,
			(float) 0.1,
			(float) 0.2,
			(float) 0.5,
			(float) 1.0,
			(float) 2.0,
			(float) 5.0,	
			};
	
	// Gui Elements
	// Settings => PanelCHA
	private JRadioButton 	RButCHA = null;		// Settings => Channel => RadioButton Channel switch
	private JRadioButton 	RButCHB = null;		// Settings => Channel => RadioButton Channel switch
	private JButton 		ButCHOnOff = null;		// Settings => Channel A => Button On/Off
	private JScrollBar 		ScrollCHHPos = null;	// Settings => Channel A => ScrollBar Horizontal Position
	private JLabel 			LabCHHPos = null;		// Settings => Channel A => Label Horizontal Position
	private JSpinner 		spinCHVoltDiv = null;	// Settings => Channel A => Spinner Horizontal Scale
	private JRadioButton 	RButCHDC = null;		// Settings => Channel A => RadioButton DC Coupling
	private JRadioButton 	RButCHAC = null;		// Settings => Channel A => RadioButton AC Coupling

	// Settings => Time
	private JScrollBar 		SBarTimeSecDiv = null;	// Settings => Time => ScrollBar Vertical Scale
	private JLabel 			LabTimeSecDiv = null;	// Settings => Time => Label Vertical Scale
	private JScrollBar 		SBarTimePosition = null;// Settings => Time => ScrollBar Vertical Position
	private JLabel 			LabTimeSec = null;		// Settings => Time => Label Vertical Position
	// Settings => Trigger
	private JComboBox 		ComboTrigCH = null;		// Settings => Trigger => JComboBox Trigger Channel
	private JComboBox 		ComboTrigMode = null;	// Settings => Trigger => JComboBox Trigger Mode
	private JComboBox 		ComboTrigEdge = null;	// Settings => Trigger => JComboBox Trigger edge
	private JLabel 			LabTrigLev = null;		// Settings => Trigger => Label Trigger Level
	private JScrollBar 		SBarTrigLev = null;		// Settings => Trigger => ScrollBar Trigger Level
	// Settings => acquire
	private JButton 		ButRunStop = null;		// Settings => acquire => Button Run/Stop
	private JButton 		ButSingleSeq = null;	// Settings => acquire => Button Single Sequence
	private JComboBox 		ComboDispMode = null;	// Settings => acquire => ComboBox Display Mode
	//Spitts the Chart from the Menu
	private JSplitPane		SplitScope;
	
	/** starts the User interface thread and trys ever 10ms to repaint.
	 * 
	 * (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		while(!Thread.currentThread().isInterrupted()&&isVisible())
		{
			if (SamplingData.getNewData(0)||SamplingData.getNewData(1))
				Chart.repaint();
			try
			{
				Thread.sleep(10);
			} catch (InterruptedException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		System.out.println("UserInterface bye");
		Control.Stop();
	}
	/**
	 * Constructor initialize every input element, make settings and so on
	 * 
	 * @param LCPDData pData sampling data
	 * @param LCPDConverterSettings pConverterSettings converter settings
	 * @param LCPDGraphicSettings pGraphicSettings graphical settings for channels, etc.
	 * @param LCPDControl pControl control interface to stop programm
	 */
	public LCPDUserInterface(LCPDData pData,
			LCPDConverterSettings pConverterSettings, 
			LCPDGraphicSettings pGraphicSettings,
			LCPDControl pControl) {
		setTitle("lcpd-scope");
		ConverterSettings=pConverterSettings;
		SamplingData=pData;
		GraphicSettings=pGraphicSettings;
		Control=pControl;
		// Tabs
		JPanel tabSettings = new JPanel();
		JPanel tabMessurement = new JPanel();
		JPanel tabExport = new JPanel();
		JTabbedPane tabbedPaneMenu = new JTabbedPane();
		tabbedPaneMenu.addTab("Settings", tabSettings);
		tabbedPaneMenu.addTab("Measurement", tabMessurement);
		tabbedPaneMenu.addTab("Export", tabExport);

		// Tab Settings
		tabSettings.setLayout(null);
		// Tab Settings => PanelCH
		{
			JPanel panelCH = new JPanel();
			tabSettings.add(panelCH);
			panelCH.setSize(200, 300);
			panelCH.setBounds(new Rectangle(10, 10, 200, 380));
			panelCH.setLayout(null); // Layoutmanager ev. noch anpassen
			panelCH.setBorder(BorderFactory.createLineBorder(Color.black));

			ButtonGroup ButCHAB = new ButtonGroup();
			RButCHA=new JRadioButton();
			RButCHA.setText("CH A");
			RButCHA.setSelected(true);
			RButCHA.setBounds(new Rectangle(10, 10, 60, 21));
			RButCHB=new JRadioButton();
			RButCHB.setText("CH B");
			RButCHB.setBounds(new Rectangle(80, 10, 60, 21));
			ButCHAB.add(RButCHA);
			ButCHAB.add(RButCHB);
			
			JLabel LabHPos = new JLabel();
			LabHPos.setBounds(new Rectangle(10, 50, 180, 30));
			LabHPos.setText("Horizontal Position");
			LabCHHPos = new JLabel();
			LabCHHPos.setBounds(new Rectangle(50, 110, 140, 30));
			LabCHHPos.setText("0 V");
			ScrollCHHPos = new JScrollBar();
			ScrollCHHPos.setBounds(new Rectangle(20, 80, 20, 100));
			ScrollCHHPos.addAdjustmentListener(this);
			ScrollCHHPos.setValues(0, 0, -2000, 2000);
			JLabel LabHScale = new JLabel();
			LabHScale.setBounds(new Rectangle(10, 180, 180, 30));
			LabHScale.setText("Horizontal Scale");
			SpinnerNumberModel spinmodHscale = new SpinnerNumberModel(5, 0, 15,
					0.2);// SpinnerModel for Horizontal Scale
			spinCHVoltDiv = new JSpinner(spinmodHscale);
			spinCHVoltDiv.setBounds(new Rectangle(20, 210, 90, 30));
			spinCHVoltDiv.addChangeListener(this);
			SpinnerModel VoltageModel = new SpinnerListModel(VoltageValues); 
			spinCHVoltDiv.setModel(VoltageModel);
			VoltageModel.setValue((float)0.2);
			JLabel LabVDiv = new JLabel();
			LabVDiv.setBounds(new Rectangle(120, 210, 70, 30));
			LabVDiv.setText("V/Div");
			JLabel LabCoppling = new JLabel();
			LabCoppling.setBounds(new Rectangle(10, 240, 180, 30));
			LabCoppling.setText("Coppling");
			RButCHAC = new JRadioButton("AC");
			RButCHAC.setBounds(new Rectangle(20, 270, 90, 30));
			RButCHAC.setSelected(true);
			RButCHAC.addActionListener(this);
			ButtonGroup ButCHACDC = new ButtonGroup();
			ButCHACDC.add(RButCHAC);
			RButCHDC = new JRadioButton("DC");
			RButCHDC.setBounds(new Rectangle(110, 270, 80, 30));
			RButCHDC.addActionListener(this);
			ButCHACDC.add(RButCHDC);
			ButCHOnOff = new JButton("On/Off");
			ButCHOnOff.setBounds(10, 310, 180, 30);
			ButCHOnOff.addActionListener(this);

			panelCH.add(RButCHA, null);
			panelCH.add(RButCHB, null);
			panelCH.add(LabHPos, null);
			panelCH.add(ScrollCHHPos, null);
			panelCH.add(LabCHHPos, null);
			panelCH.add(LabHScale, null);
			panelCH.add(spinCHVoltDiv, null);
			panelCH.add(RButCHDC, null);
			panelCH.add(RButCHAC, null);
			panelCH.add(LabVDiv, null);
			panelCH.add(LabCoppling, null);
			panelCH.add(ButCHOnOff, null);
		}
		// Tab Settings => PanelCHB
		{
			JPanel panelCHB = new JPanel();
		}

		// Tab Settings => PanelTime
		{
			JPanel panelTime = new JPanel();
			tabSettings.add(panelTime);
			// panelTime.setLayout(tabLayout);
			panelTime.setLayout(null);
			panelTime.setBounds(new Rectangle(210, 10, 200, 200));
			panelTime.setSize(200, 200);
			panelTime.setBorder(BorderFactory.createLineBorder(Color.black));

			JLabel LabVScale = new JLabel("Vertikal Scale");
			LabTimeSecDiv = new JLabel("sec/Div");
			LabTimeSecDiv.setBounds(new Rectangle(20, 70, 170, 30));
			LabVScale.setBounds(new Rectangle(10, 10, 180, 30));
			SBarTimeSecDiv = new JScrollBar(Adjustable.HORIZONTAL);
			SBarTimeSecDiv.setBounds(new Rectangle(20, 50, 170, 20));
			SBarTimeSecDiv.addAdjustmentListener(this);
			SBarTimeSecDiv.setValues(12, 0, 0, 23);
			JLabel LabVPos = new JLabel();
			LabVPos.setBounds(new Rectangle(10, 110, 180, 30));
			LabVPos.setText("Vertikal Position");
			SBarTimePosition = new JScrollBar(Adjustable.HORIZONTAL);
			SBarTimePosition.setBounds(new Rectangle(20, 150, 170, 20));
			SBarTimePosition.addAdjustmentListener(this);
			LabTimeSec = new JLabel("sec");
			LabTimeSec.setBounds(new Rectangle(20, 170, 170, 30));

			panelTime.setLayout(null);
			panelTime.add(LabTimeSecDiv, null);
			panelTime.add(LabVScale, null);
			panelTime.add(LabVPos, null);
			panelTime.add(SBarTimeSecDiv, null);
			panelTime.add(SBarTimePosition, null);
			panelTime.add(LabTimeSec, null);
		}

		// Tab Settings => PanelTrigger
		{
			JPanel panelTrigger = new JPanel();
			tabSettings.add(panelTrigger);
			panelTrigger.setLayout(null);
			panelTrigger.setBounds(new Rectangle(210, 210, 200, 280));
			panelTrigger.setSize(200, 180);
			panelTrigger.setBorder(BorderFactory.createLineBorder(Color.black));

			JLabel LabTrigger = new JLabel();
			LabTrigger.setBounds(new Rectangle(10, 10, 180, 30));
			LabTrigger.setText("Trigger");
			ComboTrigCH = new JComboBox();
			ComboTrigCH.setBounds(new Rectangle(20, 50, 130, 30));
			ComboTrigCH.addItem("CHA");
			ComboTrigCH.addItem("CHB");
			ComboTrigCH.addActionListener(this);
			ComboTrigMode = new JComboBox();
			ComboTrigMode.setBounds(new Rectangle(20, 80, 130, 30));
			ComboTrigMode.addItem("Auto");
			ComboTrigMode.addItem("Normal");
			ComboTrigMode.addActionListener(this);
			ComboTrigMode.setSelectedIndex(1);
			ComboTrigEdge = new JComboBox();
			ComboTrigEdge.setBounds(new Rectangle(20, 110, 130, 30));
			ComboTrigEdge.addItem("Rising Edge");
			ComboTrigEdge.addItem("Falling Edge");
			ComboTrigEdge.addActionListener(this);
			LabTrigLev = new JLabel("V");
			LabTrigLev.setBounds(new Rectangle(20, 140, 130, 30));
			SBarTrigLev = new JScrollBar();
			SBarTrigLev.setBounds(new Rectangle(170, 50, 20, 120));
			SBarTrigLev.addAdjustmentListener(this);
			SBarTrigLev.setValues(0, 0, -100, 100);

			panelTrigger.add(LabTrigLev, null);
			panelTrigger.add(LabTrigger, null);
			panelTrigger.add(ComboTrigCH, null);
			panelTrigger.add(ComboTrigMode, null);
			panelTrigger.add(ComboTrigEdge, null);
			panelTrigger.add(SBarTrigLev, null);
		}
		// Tab Settings => PanelAcquire
		{
			JPanel panelAcquire = new JPanel();
			tabSettings.add(panelAcquire);
			panelAcquire.setBorder(BorderFactory.createLineBorder(Color.black));
			panelAcquire.setBounds(new Rectangle(410, 10, 140, 180));
			panelAcquire.setSize(140, 170);
			panelAcquire.setLayout(null);

			JLabel LabAcquire = new JLabel("Acquire");
			LabAcquire.setBounds(new Rectangle(10, 10, 120, 30));
			ButRunStop = new JButton("Run/Stop");
			ButRunStop.addActionListener(this);
			ButRunStop.setBounds(new Rectangle(10, 50, 120, 30));
			ButSingleSeq = new JButton("Single Seq");
			ButSingleSeq.setBounds(new Rectangle(10, 90, 120, 30));
			ButSingleSeq.addActionListener(this);
			ComboDispMode = new JComboBox();
			ComboDispMode.setBounds(new Rectangle(10, 130, 120, 30));
			ComboDispMode.addActionListener(this);
			ComboDispMode.addItem("Normal");
			ComboDispMode.addItem("XY");
			ComboDispMode.addItem("FFT");

			panelAcquire.add(LabAcquire, null);
			panelAcquire.add(ButRunStop, null);
			panelAcquire.add(ButSingleSeq, null);
			panelAcquire.add(ComboDispMode, null);
		}

		// Tab Measurements
		tabMessurement.setLayout(null);
		// Tab Measurement => PanelCursor
		{
			JPanel PanelCursor = new JPanel();
			tabMessurement.add(PanelCursor);
			PanelCursor.setLayout(null);
			PanelCursor.setBounds(new Rectangle(10, 10, 500, 130));
			PanelCursor.setSize(500, 130);
			PanelCursor.setBorder(BorderFactory.createLineBorder(Color.black));

			JLabel LabMeasurements = new JLabel("Measurements");
			LabMeasurements.setBounds(new Rectangle(10, 10, 180, 30));
			JButton ButAddCursor = new JButton("Add Cursor");
			ButAddCursor.setBounds(new Rectangle(20, 50, 180, 30));
			JLabel LabActiveCursor = new JLabel("Active Cursor");
			LabActiveCursor.setBounds(new Rectangle(20, 90, 120, 30));
			JComboBox ComboActiveCursor = new JComboBox();
			ComboActiveCursor.setBounds(new Rectangle(140, 90, 120, 30));
			JButton ButDelCursor = new JButton("Del Cursor");
			ButDelCursor.setBounds(new Rectangle(260, 90, 120, 30));

			PanelCursor.add(LabMeasurements, null);
			PanelCursor.add(ButAddCursor, null);
			PanelCursor.add(LabActiveCursor, null);
			PanelCursor.add(ComboActiveCursor, null);
			PanelCursor.add(ButDelCursor, null);
		}
		// Tab Measurement => PanelCalc
		{
			JPanel PanelCalc = new JPanel();
			tabMessurement.add(PanelCalc);
			PanelCalc.setLayout(null);
			PanelCalc.setBounds(new Rectangle(10, 140, 400, 500));
			PanelCalc.setSize(500, 400);
			PanelCalc.setBorder(BorderFactory.createLineBorder(Color.black));

			JLabel LabCalc = new JLabel("Calc");
			LabCalc.setBounds(new Rectangle(10, 10, 180, 30));
			JLabel LabCursor1 = new JLabel("Cursor 1");
			LabCursor1.setBounds(new Rectangle(100, 40, 100, 30));
			JLabel LabOperation = new JLabel("Operation");
			LabOperation.setBounds(new Rectangle(200, 40, 100, 30));
			JLabel LabCursor2 = new JLabel("Cursor 2");
			LabCursor2.setBounds(new Rectangle(300, 40, 100, 30));
			JLabel LabCalc1 = new JLabel("Calc 1 :");
			LabCalc1.setBounds(new Rectangle(20, 70, 80, 30));
			JComboBox ComboCursor11 = new JComboBox();
			ComboCursor11.setBounds(new Rectangle(100, 70, 100, 30));
			JComboBox ComboOperation1 = new JComboBox();
			ComboOperation1.setBounds(new Rectangle(200, 70, 100, 30));
			JComboBox ComboCursor21 = new JComboBox();
			ComboCursor21.setBounds(new Rectangle(300, 70, 100, 30));
			JButton ButCalcSave1 = new JButton("Save");
			ButCalcSave1.setBounds(new Rectangle(400, 70, 80, 30));
			PanelCalc.add(LabCalc1, null);
			PanelCalc.add(LabCursor1, null);
			PanelCalc.add(LabOperation, null);
			PanelCalc.add(LabCursor2, null);
			PanelCalc.add(LabCalc, null);
			PanelCalc.add(ComboOperation1, null);
			PanelCalc.add(ComboCursor11, null);
			PanelCalc.add(ComboCursor21, null);
			PanelCalc.add(ButCalcSave1, null);
		}
		// Tab Export => Export
		tabExport.setLayout(null);
		{
			JPanel PanelExport = new JPanel();
			tabExport.add(PanelExport);
			PanelExport.setLayout(null);
			PanelExport.setBounds(new Rectangle(10, 10, 400, 500));
			PanelExport.setSize(500, 400);
			// PanelExport.setBorder(BorderFactory.createLineBorder(Color.black));
			JLabel LabExport = new JLabel("Export");
			LabExport.setBounds(10, 10, 200, 30);
			JButton ButExportAs = new JButton("Export As...");
			ButExportAs.setBounds(10, 50, 200, 30);

			PanelExport.add(LabExport);
			PanelExport.add(ButExportAs);
		}
		SplitScope = new JSplitPane();
		SplitScope.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
		SplitScope.setContinuousLayout(true);
		SplitScope.setDividerLocation(500);
		Chart=new LCPDChart(SamplingData,ConverterSettings, GraphicSettings);
		Chart.setSize(200,200);
		setContentPane (SplitScope);
		SplitScope.setLeftComponent(Chart);
		SplitScope.setRightComponent(tabbedPaneMenu);

		setSize(1200, 500);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	public void mousePressed(MouseEvent e) {
	}

	/**
	 * if an action Performed shows which element it was
	 * 
	 * @param ActionEvent e event who performed it
	 * (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		if(ButCHOnOff == e.getSource())	// Settings => Channel A => Button On/Off
		{
			if (RButCHA.isSelected())
				GraphicSettings.setDisplayChannel(0, !GraphicSettings.getDisplayChannel(0));
			else if (RButCHB.isSelected())
				GraphicSettings.setDisplayChannel(1, !GraphicSettings.getDisplayChannel(1));
		}
		else if (RButCHAC == e.getSource())// Settings => Channel A => RadioButton AC Coupling
		{
			if (RButCHA.isSelected())
				ConverterSettings.setCouplingMode(0,LCPDConverterSettings.LCPDCouplingMode.AC);
			else if (RButCHB.isSelected())
				ConverterSettings.setCouplingMode(1,LCPDConverterSettings.LCPDCouplingMode.AC);
		}
		else if (RButCHDC == e.getSource())// Settings => Channel A => RadioButton DC Coupling
		{
			if (RButCHA.isSelected())
				ConverterSettings.setCouplingMode(0,LCPDConverterSettings.LCPDCouplingMode.DC);
			else if (RButCHB.isSelected())
				ConverterSettings.setCouplingMode(1,LCPDConverterSettings.LCPDCouplingMode.DC);
		}
		else if (ComboTrigCH == e.getSource()) {// Settings => Trigger => JComboBox Trigger Channel
			System.out.println(ComboTrigCH.getSelectedItem());}
		else if (ComboTrigMode == e.getSource()) {// Settings => Trigger => JComboBox Trigger Mode
			if(ComboTrigMode.getSelectedItem().equals("Normal"))
				ConverterSettings.setTriggerMode(LCPDTriggerMode.Normal);
			else if(ComboTrigMode.getSelectedItem().equals("Auto"))
				ConverterSettings.setTriggerMode(LCPDTriggerMode.Auto);
			else if (ComboTrigMode.getSelectedItem().equals("SingleSlope"))
				ConverterSettings.setTriggerMode(LCPDTriggerMode.SingleSlope);}
		else if (ComboTrigEdge == e.getSource()) {
			if(ComboTrigEdge.getSelectedItem().equals("Rising Edge"))
				ConverterSettings.setTriggerEdge(LCPDTriggerEdge.positive);
			else if (ComboTrigEdge.getSelectedItem().equals("Falling Edge"))
				ConverterSettings.setTriggerEdge(LCPDTriggerEdge.negative);}
		else if(ButRunStop== e.getSource())	// Settings => acquire => Button Run/Stop
			ConverterSettings.setRun(!ConverterSettings.isRun());
		else if(ButSingleSeq == e.getSource())	// Settings => acquire => Button Single Sequence
			System.out.println("Settings => acquire => Button Single Sequence");
		else if (ComboDispMode == e.getSource()) {//Settings => acquire => ComboBox Display Mode
			System.out.println(ComboDispMode.getSelectedItem());}
	}
	
	/**
	 * if a Value of an element changed
	 * 
	 * @param AdjustmentEvent e event settings
	 * (non-Javadoc)
	 * @see java.awt.event.AdjustmentListener#adjustmentValueChanged(java.awt.event.AdjustmentEvent)
	 */
	public void adjustmentValueChanged(AdjustmentEvent e) {
		if(ScrollCHHPos == e.getSource()) {	// Settings => Channel A => ScrollBar Horizontal Position
			float OffsetValue=-2*ConverterSettings.getMaxVoltage(0)/2000*ScrollCHHPos.getValue();
			LabCHHPos.setText(OffsetValue + " V");	// Set Settings => Channel A => Label Horizontal Position
			if (RButCHA.isSelected())
				GraphicSettings.setOffsetVoltage(0, OffsetValue);
			else if(RButCHB.isSelected())
				GraphicSettings.setOffsetVoltage(1, OffsetValue);
			System.out.println("Settings => Channel A => ScrollBar Horizontal Position, Val: "+ScrollCHHPos.getValue());}
		else if (SBarTimeSecDiv == e.getSource()) {	// Settings => Time => ScrollBar Vertical Scale		
			float TimeValue=TimeValues[SBarTimeSecDiv.getValue()];
			LabTimeSecDiv.setText(TimeValue + " sec/Div");
			ConverterSettings.setDisplayTime(TimeValue*10);
			System.out.println("Settings => Time => ScrollBar Vertical Scale");}
		else if (SBarTimePosition == e.getSource()) {	// Settings => Time => ScrollBar Vertical Position	
			LabTimeSec.setText(SBarTimePosition.getValue() + " sec");
			System.out.println("Settings => Time => ScrollBar Vertical Position");}
		else if (SBarTrigLev == e.getSource()) {	// Settings => Trigger => ScrollBar Trigger Level
			LabTrigLev.setText(SBarTrigLev.getValue() + " V");
			System.out.println("Settings => Trigger => ScrollBar Trigger Level");}	
	}

	/**
	 * if a state changed somewhere
	 * 
	 * @param ChangeEvent e which event
	 * (non-Javadoc)
	 * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
	 */
	public void stateChanged(ChangeEvent e) {
		if(spinCHVoltDiv == e.getSource())	// Settings => Channel A => Spinner Horizontal Scale
		{
			float VoltageValue;
			VoltageValue=(Float) spinCHVoltDiv.getValue();
			if (RButCHA.isSelected())
				ConverterSettings.setMaxVoltage(0, VoltageValue*10);
			else if (RButCHB.isSelected())
				ConverterSettings.setMaxVoltage(1, VoltageValue*10);
		}			
	}
}